import axios from "axios";
import type { AxiosInstance } from 'axios'

import type { YBRequestConfig, YBRequestInterceptors } from './type'
import { ElLoading, ILoadingInstance } from 'element-plus'

const DEFAULT_LOADING = true;

class YBRequest {
  instance: AxiosInstance
  interceptors?: YBRequestInterceptors
  showLoading: boolean
  loading?: ILoadingInstance

  constructor(config: YBRequestConfig) {
    this.instance = axios.create(config);
    this.showLoading = config.showLoading ?? true;
    this.interceptors = config.interceptors;

    //1.从config中取出的拦截器是对应的实例的拦截器
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch)
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )

    //2.添加所有的实例都有的拦截器
    this.instance.interceptors.request.use(
      config => {
        console.log('所有请求成功');

        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在请求数据...',
            background: 'rgba(0,0,0,0.5)'
          });
        }
        return config;
      }, err => {
        return err;
      })

    this.instance.interceptors.response.use(
      res => {
        //将loading移除
        this.loading?.close();

        const data = res.data;
        if (data.returnCode === '405') {
           console.log('405 method not allowed');
        } else {
          return data;
        }
      }, err => {
        this.loading?.close();
        if (err.response.status === 404) {
           console.log('404 not found');
        }
        return err;
      })
  }

  request<T = any>(config: YBRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors.requestInterceptor(config);
      }

      if (config.showLoading === false) {
        this.showLoading = !DEFAULT_LOADING;
      }

      this.instance
      .request<any,T>(config)
      .then(res => {
        //单个请求对数据的处理
        if (config.interceptors?.responseInterceptor) {
          res = config.interceptors.responseInterceptor(res);
        }
        //将showLoading设置true,这样不会影响下一个请求
        // console.log(res);
        this.showLoading = DEFAULT_LOADING;
        resolve(res);
      })
        .catch(err => {
          this.showLoading = DEFAULT_LOADING;
          // return err;
          reject(err);
        })
    })
  }

  get<T = any>(config: YBRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }

  post<T = any>(config: YBRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }

  delete<T = any>(config: YBRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }

  patch<T = any>(config: YBRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' })
  }
}
export default YBRequest;
